package com.sftz.modules.minapp.user.module;

import java.util.Map;

import javax.servlet.http.HttpSession;

import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Filters;
import org.nutz.mvc.annotation.Param;

import com.sftz.framework.rersult.MobileResult;
import com.sftz.modules.common.form.UserBandTelForm;
import com.sftz.modules.minapp.user.form.CaptchaForm;
import com.sftz.modules.minapp.user.form.LoginTokenForm;
import com.sftz.modules.minapp.user.form.WxMaUserForm;
import com.sftz.modules.minapp.user.service.WxMaUserViewService;

/**
 * 微信小程序用户接口
 */
@IocBean
@At("/wx/user")
@Filters
public class WxMaUserModule {

	@Inject
	private WxMaUserViewService wxMaUserViewService;

	/**
	 *	使用code换取openid和sessionKey
	 */
	@At
	public Object requestToken(final @Param("..") LoginTokenForm form, HttpSession session) {
		return wxMaUserViewService.getSessionKey(form, session);
	}

	/**
	 * <pre>
	 * 登录,获取用户信息接口
	 * </pre>
	 */
	@At
	public Object login(@Param("..") final WxMaUserForm form) {
		return wxMaUserViewService.login(form);
	}

	/**
	 * 检查手机号是否存在
	 */
	@At
	public Object checkTel(@Param("tel") final String tel) {
		try {
			return wxMaUserViewService.checkTel(tel);
		} catch (Exception e) {
			e.printStackTrace();
			return MobileResult.error("接口调用失败", null);
		}
	}

	/**
	 * <pre>
	 * 绑定手机号接口
	 * </pre>
	 */
	@At
	public Object bandTel(@Param("..") final UserBandTelForm form) {
		try {
			return wxMaUserViewService.bandTel(form);
		} catch (Exception e) {
			e.printStackTrace();
			return MobileResult.error("接口调用失败", null);
		}
	}

	/**
	 * 发送手机验证码
	 */
	@At
	public Map<String, Object> sendCaptcha(@Param("tel") final String tel) {
		try {
			return wxMaUserViewService.sendCaptcha(tel);
		} catch (Exception e) {
			e.printStackTrace();
			return MobileResult.error("接口调用失败", null);
		}
	}

	/**
	 * 检查验证码是否正确
	 */
	@At
	public Map<String, Object> checkCaptcha(@Param("..") final CaptchaForm form) {
		try {
			return wxMaUserViewService.checkCaptcha(form);
		} catch (Exception e) {
			e.printStackTrace();
			return MobileResult.error("接口调用失败", null);
		}
	}

}
